package li.tmi.TxBtry;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class TxBtryReceiver extends BroadcastReceiver{
	static String TAG = "tmi.li.txbtryreceiver";
	private TxPref txpref; 

	@Override
	public void onReceive(Context context, Intent intent) {


		//getting the Broadcast Action to verify the intent Actions
		String action = intent.getAction();
		Log.d(TAG, "intent received : "+action);

		System.out.println(intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID));
		if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
			AppWidgetManager gm = AppWidgetManager.getInstance(context);
            int[] appWidgetIds = gm.getAppWidgetIds(new ComponentName(TxBtryFinals.strPackageName, TxBtryFinals.strWidgetClassPath));
			
			
			for(int i = 0; i < appWidgetIds.length; i++) {
				checkBatteryLevel(context, appWidgetIds[i], intent);
				System.out.println("checked battery level");
				WidgetProvider.updateWidget(context, gm,  appWidgetIds[i]);
			}
		}
	}

	public void checkBatteryLevel(Context context, int id, Intent intent) {
		txpref = new TxPref(TxPref.PREFS_NAME_TXBTRY);
		
		//check for first run or if Plugged status changed
		if(txpref.getBtryMisc(context, id, TxPref.BTRYLEVEL) == null ||
		   txpref.getBtryMisc(context, id, TxPref.BTRYPLUG) != ((intent.getIntExtra(TxBtryFinals.strBatteryPluggedProperty, 0) == 0) ? "false" : "true")) {

			txpref.saveBtryMisc(context, id, TxPref.BTRYLEVEL, String.valueOf(intent.getIntExtra(TxBtryFinals.strBatteryLevelProperty, 0)));
			txpref.saveBtryMisc(context, id, TxPref.BTRYTIME, String.valueOf(System.currentTimeMillis()));
			txpref.saveBtryMisc(context, id, TxPref.BTRYRTIME, String.valueOf(0));
			txpref.saveBtryMisc(context, id, TxPref.BTRYPLUG, (intent.getIntExtra(TxBtryFinals.strBatteryPluggedProperty, 0) == 0) ? "false" : "true");
	
		}else {
			
			int startLevel = Integer.valueOf(txpref.getBtryMisc(context, id, TxPref.BTRYLEVEL));
			int currentLevel = intent.getIntExtra(TxBtryFinals.strBatteryLevelProperty, 0);
			boolean isPlugged = (intent.getIntExtra(TxBtryFinals.strBatteryPluggedProperty, 0) == 0) ? false : true;
			long startTime = Long.valueOf(txpref.getBtryMisc(context, id, TxPref.BTRYTIME)); 
			long restTime;
			double tmp;
			//check if device is charging
			if(isPlugged) {
				//(100-current)/((current-start)/time)  
				tmp = (double)(currentLevel-startLevel);
				tmp = tmp/(double)(System.currentTimeMillis()-startTime);
				restTime = (long)((100-currentLevel)/tmp);

			}else {
				tmp = (double)(startLevel-currentLevel);
				tmp = tmp / ((double)(System.currentTimeMillis() - startTime));
				restTime = (long)(currentLevel / tmp);
			}
			txpref.saveBtryMisc(context, id, TxPref.BTRYCLEVEL, String.valueOf(currentLevel));
			txpref.saveBtryMisc(context, id, TxPref.BTRYRTIME, String.valueOf(restTime));
			txpref.saveBtryMisc(context, id, TxPref.BTRYPLUG, String.valueOf(isPlugged));
		}
	}

}